home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / gdb-4.12 / gdb / command.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  34.3 KB  |  1,311 lines

  1. /* Handle lists of commands, their decoding and documentation, for GDB.
  2.    Copyright 1986, 1989, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include "defs.h"
  19. #include "gdbcmd.h"
  20. #include "symtab.h"
  21. #include "value.h"
  22. #include <ctype.h>
  23. #include <string.h>
  24.  
  25. /* Prototypes for local functions */
  26.  
  27. static void
  28. undef_cmd_error PARAMS ((char *, char *));
  29.  
  30. static void
  31. show_user PARAMS ((char *, int));
  32.  
  33. static void
  34. show_user_1 PARAMS ((struct cmd_list_element *, GDB_FILE *));
  35.  
  36. static void
  37. make_command PARAMS ((char *, int));
  38.  
  39. static void
  40. shell_escape PARAMS ((char *, int));
  41.  
  42. static int
  43. parse_binary_operation PARAMS ((char *));
  44.  
  45. static void
  46. print_doc_line PARAMS ((GDB_FILE *, char *));
  47.  
  48. /* Add element named NAME.
  49.    CLASS is the top level category into which commands are broken down
  50.    for "help" purposes.
  51.    FUN should be the function to execute the command;
  52.    it will get a character string as argument, with leading
  53.    and trailing blanks already eliminated.
  54.  
  55.    DOC is a documentation string for the command.
  56.    Its first line should be a complete sentence.
  57.    It should start with ? for a command that is an abbreviation
  58.    or with * for a command that most users don't need to know about.
  59.  
  60.    Add this command to command list *LIST.  */
  61.  
  62. struct cmd_list_element *
  63. add_cmd (name, class, fun, doc, list)
  64.      char *name;
  65.      enum command_class class;
  66.      void (*fun) PARAMS ((char *, int));
  67.      char *doc;
  68.      struct cmd_list_element **list;
  69. {
  70.   register struct cmd_list_element *c
  71.     = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
  72.  
  73.   delete_cmd (name, list);
  74.   c->next = *list;
  75.   c->name = name;
  76.   c->class = class;
  77.   c->function.cfunc = fun;
  78.   c->doc = doc;
  79.   c->prefixlist = 0;
  80.   c->prefixname = (char *)NULL;
  81.   c->allow_unknown = 0;
  82.   c->hook = 0;
  83.   c->hookee = 0;
  84.   c->cmd_pointer = 0;
  85.   c->abbrev_flag = 0;
  86.   c->type = not_set_cmd;
  87.   c->completer = make_symbol_completion_list;
  88.   c->var = 0;
  89.   c->var_type = var_boolean;
  90.   c->user_commands = 0;
  91.   *list = c;
  92.   return c;
  93. }
  94.  
  95. /* Same as above, except that the abbrev_flag is set. */
  96.  
  97. #if 0    /* Currently unused */
  98.  
  99. struct cmd_list_element *
  100. add_abbrev_cmd (name, class, fun, doc, list)
  101.      char *name;
  102.      enum command_class class;
  103.      void (*fun) PARAMS ((char *, int));
  104.      char *doc;
  105.      struct cmd_list_element **list;
  106. {
  107.   register struct cmd_list_element *c
  108.     = add_cmd (name, class, fun, doc, list);
  109.  
  110.   c->abbrev_flag = 1;
  111.   return c;
  112. }
  113.  
  114. #endif
  115.  
  116. struct cmd_list_element *
  117. add_alias_cmd (name, oldname, class, abbrev_flag, list)
  118.      char *name;
  119.      char *oldname;
  120.      enum command_class class;
  121.      int abbrev_flag;
  122.      struct cmd_list_element **list;
  123. {
  124.   /* Must do this since lookup_cmd tries to side-effect its first arg */
  125.   char *copied_name;
  126.   register struct cmd_list_element *old;
  127.   register struct cmd_list_element *c;
  128.   copied_name = (char *) alloca (strlen (oldname) + 1);
  129.   strcpy (copied_name, oldname);
  130.   old  = lookup_cmd (&copied_name, *list, "", 1, 1);
  131.  
  132.   if (old == 0)
  133.     {
  134.       delete_cmd (name, list);
  135.       return 0;
  136.     }
  137.  
  138.   c = add_cmd (name, class, old->function.cfunc, old->doc, list);
  139.   c->prefixlist = old->prefixlist;
  140.   c->prefixname = old->prefixname;
  141.   c->allow_unknown = old->allow_unknown;
  142.   c->abbrev_flag = abbrev_flag;
  143.   c->cmd_pointer = old;
  144.   return c;
  145. }
  146.  
  147. /* Like add_cmd but adds an element for a command prefix:
  148.    a name that should be followed by a subcommand to be looked up
  149.    in another command list.  PREFIXLIST should be the address
  150.    of the variable containing that list.  */
  151.  
  152. struct cmd_list_element *
  153. add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
  154.         allow_unknown, list)
  155.      char *name;
  156.      enum command_class class;
  157.      void (*fun) PARAMS ((char *, int));
  158.      char *doc;
  159.      struct cmd_list_element **prefixlist;
  160.      char *prefixname;
  161.      int allow_unknown;
  162.      struct cmd_list_element **list;
  163. {
  164.   register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
  165.   c->prefixlist = prefixlist;
  166.   c->prefixname = prefixname;
  167.   c->allow_unknown = allow_unknown;
  168.   return c;
  169. }
  170.  
  171. /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
  172.    
  173. struct cmd_list_element *
  174. add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
  175.                allow_unknown, list)
  176.      char *name;
  177.      enum command_class class;
  178.      void (*fun) PARAMS ((char *, int));
  179.      char *doc;
  180.      struct cmd_list_element **prefixlist;
  181.      char *prefixname;
  182.      int allow_unknown;
  183.      struct cmd_list_element **list;
  184. {
  185.   register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
  186.   c->prefixlist = prefixlist;
  187.   c->prefixname = prefixname;
  188.   c->allow_unknown = allow_unknown;
  189.   c->abbrev_flag = 1;
  190.   return c;
  191. }
  192.  
  193. /* ARGSUSED */
  194. void
  195. not_just_help_class_command (args, from_tty)
  196.      char *args;
  197.      int from_tty;
  198. {
  199. }
  200.  
  201. /* Add element named NAME to command list LIST (the list for set
  202.    or some sublist thereof).
  203.    CLASS is as in add_cmd.
  204.    VAR_TYPE is the kind of thing we are setting.
  205.    VAR is address of the variable being controlled by this command.
  206.    DOC is the documentation string.  */
  207.  
  208. struct cmd_list_element *
  209. add_set_cmd (name, class, var_type, var, doc, list)
  210.      char *name;
  211.      enum command_class class;
  212.      var_types var_type;
  213.      char *var;
  214.      char *doc;
  215.      struct cmd_list_element **list;
  216. {
  217.   /* For set/show, we have to call do_setshow_command
  218.      differently than an ordinary function (take commandlist as
  219.      well as arg), so the function field isn't helpful.  However,
  220.      function == NULL means that it's a help class, so set the function
  221.      to not_just_help_class_command.  */
  222.   struct cmd_list_element *c
  223.     = add_cmd (name, class, not_just_help_class_command, doc, list);
  224.  
  225.   c->type = set_cmd;
  226.   c->var_type = var_type;
  227.   c->var = var;
  228.   return c;
  229. }
  230.  
  231. /* Where SETCMD has already been added, add the corresponding show
  232.    command to LIST and return a pointer to it.  */
  233. struct cmd_list_element *
  234. add_show_from_set (setcmd, list)
  235.      struct cmd_list_element *setcmd;
  236.      struct cmd_list_element **list;
  237. {
  238.   struct cmd_list_element *showcmd =
  239.     (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
  240.  
  241.   memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
  242.   delete_cmd (showcmd->name, list);
  243.   showcmd->type = show_cmd;
  244.   
  245.   /* Replace "set " at start of docstring with "show ".  */
  246.   if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
  247.       && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
  248.     showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
  249.   else
  250.     fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
  251.   
  252.   showcmd->next = *list;
  253.   *list = showcmd;
  254.   return showcmd;
  255. }
  256.  
  257. /* Remove the command named NAME from the command list.  */
  258.  
  259. void
  260. delete_cmd (name, list)
  261.      char *name;
  262.      struct cmd_list_element **list;
  263. {
  264.   register struct cmd_list_element *c;
  265.   struct cmd_list_element *p;
  266.  
  267.   while (*list && STREQ ((*list)->name, name))
  268.     {
  269.       if ((*list)->hookee)
  270.     (*list)->hookee->hook = 0;    /* Hook slips out of its mouth */
  271.       p = (*list)->next;
  272.       free ((PTR)*list);
  273.       *list = p;
  274.     }
  275.  
  276.   if (*list)
  277.     for (c = *list; c->next;)
  278.       {
  279.     if (STREQ (c->next->name, name))
  280.       {
  281.         if (c->next->hookee)
  282.           c->next->hookee->hook = 0;  /* hooked cmd gets away.  */
  283.         p = c->next->next;
  284.         free ((PTR)c->next);
  285.         c->next = p;
  286.       }
  287.     else
  288.       c = c->next;
  289.       }
  290. }
  291.  
  292. /* This command really has to deal with two things:
  293.  *     1) I want documentation on *this string* (usually called by
  294.  * "help commandname").
  295.  *     2) I want documentation on *this list* (usually called by
  296.  * giving a command that requires subcommands.  Also called by saying
  297.  * just "help".)
  298.  *
  299.  *   I am going to split this into two seperate comamnds, help_cmd and
  300.  * help_list. 
  301.  */
  302.  
  303. void
  304. help_cmd (command, stream)
  305.      char *command;
  306.      GDB_FILE *stream;
  307. {
  308.   struct cmd_list_element *c;
  309.   extern struct cmd_list_element *cmdlist;
  310.  
  311.   if (!command)
  312.     {
  313.       help_list (cmdlist, "", all_classes, stream);
  314.       return;
  315.     }
  316.  
  317.   c = lookup_cmd (&command, cmdlist, "", 0, 0);
  318.  
  319.   if (c == 0)
  320.     return;
  321.  
  322.   /* There are three cases here.
  323.      If c->prefixlist is nonzero, we have a prefix command.
  324.      Print its documentation, then list its subcommands.
  325.      
  326.      If c->function is nonzero, we really have a command.
  327.      Print its documentation and return.
  328.      
  329.      If c->function is zero, we have a class name.
  330.      Print its documentation (as if it were a command)
  331.      and then set class to the number of this class
  332.      so that the commands in the class will be listed.  */
  333.  
  334.   fputs_filtered (c->doc, stream);
  335.   fputs_filtered ("\n", stream);
  336.  
  337.   if (c->prefixlist == 0 && c->function.cfunc != NULL)
  338.     return;
  339.   fprintf_filtered (stream, "\n");
  340.  
  341.   /* If this is a prefix command, print it's subcommands */
  342.   if (c->prefixlist)
  343.     help_list (*c->prefixlist, c->prefixname, all_commands, stream);
  344.  
  345.   /* If this is a class name, print all of the commands in the class */
  346.   if (c->function.cfunc == NULL)
  347.     help_list (cmdlist, "", c->class, stream);
  348.  
  349.   if (c->hook)
  350.     fprintf_filtered (stream, "\nThis command has a hook defined: %s\n",
  351.               c->hook->name);
  352. }
  353.  
  354. /*
  355.  * Get a specific kind of help on a command list.
  356.  *
  357.  * LIST is the list.
  358.  * CMDTYPE is the prefix to use in the title string.
  359.  * CLASS is the class with which to list the nodes of this list (see
  360.  * documentation for help_cmd_list below),  As usual, ALL_COMMANDS for
  361.  * everything, ALL_CLASSES for just classes, and non-negative for only things
  362.  * in a specific class.
  363.  * and STREAM is the output stream on which to print things.
  364.  * If you call this routine with a class >= 0, it recurses.
  365.  */
  366. void
  367. help_list (list, cmdtype, class, stream)
  368.      struct cmd_list_element *list;
  369.      char *cmdtype;
  370.      enum command_class class;
  371.      GDB_FILE *stream;
  372. {
  373.   int len;
  374.   char *cmdtype1, *cmdtype2;
  375.   
  376.   /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub"  */
  377.   len = strlen (cmdtype);
  378.   cmdtype1 = (char *) alloca (len + 1);
  379.   cmdtype1[0] = 0;
  380.   cmdtype2 = (char *) alloca (len + 4);
  381.   cmdtype2[0] = 0;
  382.   if (len)
  383.     {
  384.       cmdtype1[0] = ' ';
  385.       strncpy (cmdtype1 + 1, cmdtype, len - 1);
  386.       cmdtype1[len] = 0;
  387.       strncpy (cmdtype2, cmdtype, len - 1);
  388.       strcpy (cmdtype2 + len - 1, " sub");
  389.     }
  390.  
  391.   if (class == all_classes)
  392.     fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
  393.   else
  394.     fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
  395.  
  396.   help_cmd_list (list, class, cmdtype, (int)class >= 0, stream);
  397.  
  398.   if (class == all_classes)
  399.     fprintf_filtered (stream, "\n\
  400. Type \"help%s\" followed by a class name for a list of commands in that class.",
  401.          cmdtype1);
  402.  
  403.   fprintf_filtered (stream, "\n\
  404. Type \"help%s\" followed by %scommand name for full documentation.\n\
  405. Command name abbreviations are allowed if unambiguous.\n",
  406.        cmdtype1, cmdtype2);
  407. }
  408.      
  409. /* Print only the first line of STR on STREAM.  */
  410. static void
  411. print_doc_line (stream, str)
  412.      GDB_FILE *stream;
  413.      char *str;
  414. {
  415.   static char *line_buffer = 0;
  416.   static int line_size;
  417.   register char *p;
  418.  
  419.   if (!line_buffer)
  420.     {
  421.       line_size = 80;
  422.       line_buffer = (char *) xmalloc (line_size);
  423.     }
  424.  
  425.   p = str;
  426.   while (*p && *p != '\n' && *p != '.' && *p != ',')
  427.     p++;
  428.   if (p - str > line_size - 1)
  429.     {
  430.       line_size = p - str + 1;
  431.       free ((PTR)line_buffer);
  432.       line_buffer = (char *) xmalloc (line_size);
  433.     }
  434.   strncpy (line_buffer, str, p - str);
  435.   line_buffer[p - str] = '\0';
  436.   if (islower (line_buffer[0]))
  437.     line_buffer[0] = toupper (line_buffer[0]);
  438.   fputs_filtered (line_buffer, stream);
  439. }
  440.  
  441. /*
  442.  * Implement a help command on command list LIST.
  443.  * RECURSE should be non-zero if this should be done recursively on
  444.  * all sublists of LIST.
  445.  * PREFIX is the prefix to print before each command name.
  446.  * STREAM is the stream upon which the output should be written.
  447.  * CLASS should be:
  448.  *    A non-negative class number to list only commands in that
  449.  * class.
  450.  *    ALL_COMMANDS to list all commands in list.
  451.  *    ALL_CLASSES  to list all classes in list.
  452.  *
  453.  *   Note that RECURSE will be active on *all* sublists, not just the
  454.  * ones selected by the criteria above (ie. the selection mechanism
  455.  * is at the low level, not the high-level).
  456.  */
  457. void
  458. help_cmd_list (list, class, prefix, recurse, stream)
  459.      struct cmd_list_element *list;
  460.      enum command_class class;
  461.      char *prefix;
  462.      int recurse;
  463.      GDB_FILE *stream;
  464. {
  465.   register struct cmd_list_element *c;
  466.  
  467.   for (c = list; c; c = c->next)
  468.     {
  469.       if (c->abbrev_flag == 0 &&
  470.       (class == all_commands
  471.       || (class == all_classes && c->function.cfunc == NULL)
  472.       || (class == c->class && c->function.cfunc != NULL)))
  473.     {
  474.       fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
  475.       print_doc_line (stream, c->doc);
  476.       fputs_filtered ("\n", stream);
  477.     }
  478.       if (recurse
  479.       && c->prefixlist != 0
  480.       && c->abbrev_flag == 0)
  481.     help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
  482.     }
  483. }
  484.  
  485. /* This routine takes a line of TEXT and a CLIST in which to start the
  486.    lookup.  When it returns it will have incremented the text pointer past
  487.    the section of text it matched, set *RESULT_LIST to point to the list in
  488.    which the last word was matched, and will return a pointer to the cmd
  489.    list element which the text matches.  It will return NULL if no match at
  490.    all was possible.  It will return -1 (cast appropriately, ick) if ambigous
  491.    matches are possible; in this case *RESULT_LIST will be set to point to
  492.    the list in which there are ambiguous choices (and *TEXT will be set to
  493.    the ambiguous text string).
  494.  
  495.    If the located command was an abbreviation, this routine returns the base
  496.    command of the abbreviation.
  497.  
  498.    It does no error reporting whatsoever; control will always return
  499.    to the superior routine.
  500.  
  501.    In the case of an ambiguous return (-1), *RESULT_LIST will be set to point
  502.    at the prefix_command (ie. the best match) *or* (special case) will be NULL
  503.    if no prefix command was ever found.  For example, in the case of "info a",
  504.    "info" matches without ambiguity, but "a" could be "args" or "address", so
  505.    *RESULT_LIST is set to the cmd_list_element for "info".  So in this case
  506.    RESULT_LIST should not be interpeted as a pointer to the beginning of a
  507.    list; it simply points to a specific command.  In the case of an ambiguous
  508.    return *TEXT is advanced past the last non-ambiguous prefix (e.g.
  509.    "info t" can be "info types" or "info target"; upon return *TEXT has been
  510.    advanced past "info ").
  511.  
  512.    If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise
  513.    affect the operation).
  514.  
  515.    This routine does *not* modify the text pointed to by TEXT.
  516.    
  517.    If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which
  518.    are actually help classes rather than commands (i.e. the function field of
  519.    the struct cmd_list_element is NULL).  */
  520.  
  521. struct cmd_list_element *
  522. lookup_cmd_1 (text, clist, result_list, ignore_help_classes)
  523.      char **text;
  524.      struct cmd_list_element *clist, **result_list;
  525.      int ignore_help_classes;
  526. {
  527.   char *p, *command;
  528.   int len, tmp, nfound;
  529.   struct cmd_list_element *found, *c;
  530.  
  531.   while (**text == ' ' || **text == '\t')
  532.     (*text)++;
  533.  
  534.   /* Treating underscores as part of command words is important
  535.      so that "set args_foo()" doesn't get interpreted as
  536.      "set args _foo()".  */
  537.   for (p = *text;
  538.        *p && (isalnum(*p) || *p == '-' || *p == '_');
  539.        p++)
  540.     ;
  541.  
  542.   /* If nothing but whitespace, return 0.  */
  543.   if (p == *text)
  544.     return 0;
  545.   
  546.   len = p - *text;
  547.  
  548.   /* *text and p now bracket the first command word to lookup (and
  549.      it's length is len).  We copy this into a local temporary,
  550.      converting to lower case as we go.  */
  551.  
  552.   command = (char *) alloca (len + 1);
  553.   for (tmp = 0; tmp < len; tmp++)
  554.     {
  555.       char x = (*text)[tmp];
  556.       command[tmp] = isupper(x) ? tolower(x) : x;
  557.     }
  558.   command[len] = '\0';
  559.  
  560.   /* Look it up.  */
  561.   found = 0;
  562.   nfound = 0;
  563.   for (c = clist; c; c = c->next)
  564.     if (!strncmp (command, c->name, len)
  565.     && (!ignore_help_classes || c->function.cfunc))
  566.       {
  567.     found = c;
  568.     nfound++;
  569.     if (c->name[len] == '\0')
  570.       {
  571.         nfound = 1;
  572.         break;
  573.       }
  574.       }
  575.  
  576.   /* If nothing matches, we have a simple failure.  */
  577.   if (nfound == 0)
  578.     return 0;
  579.  
  580.   if (nfound > 1)
  581.     {
  582.       if (result_list != NULL)
  583.     /* Will be modified in calling routine
  584.        if we know what the prefix command is.  */
  585.     *result_list = 0;        
  586.       return (struct cmd_list_element *) -1; /* Ambiguous.  */
  587.     }
  588.  
  589.   /* We've matched something on this list.  Move text pointer forward. */
  590.  
  591.   *text = p;
  592.  
  593.   /* If this was an abbreviation, use the base command instead.  */
  594.  
  595.   if (found->cmd_pointer)
  596.     found = found->cmd_pointer;
  597.  
  598.   /* If we found a prefix command, keep looking.  */
  599.  
  600.   if (found->prefixlist)
  601.     {
  602.       c = lookup_cmd_1 (text, *found->prefixlist, result_list,
  603.             ignore_help_classes);
  604.       if (!c)
  605.     {
  606.       /* Didn't find anything; this is as far as we got.  */
  607.       if (result_list != NULL)
  608.         *result_list = clist;
  609.       return found;
  610.     }
  611.       else if (c == (struct cmd_list_element *) -1)
  612.     {
  613.       /* We've gotten this far properley, but the next step
  614.          is ambiguous.  We need to set the result list to the best
  615.          we've found (if an inferior hasn't already set it).  */
  616.       if (result_list != NULL)
  617.         if (!*result_list)
  618.           /* This used to say *result_list = *found->prefixlist
  619.          If that was correct, need to modify the documentation
  620.          at the top of this function to clarify what is supposed
  621.          to be going on.  */
  622.           *result_list = found;
  623.       return c;
  624.     }
  625.       else
  626.     {
  627.       /* We matched!  */
  628.       return c;
  629.     }
  630.     }
  631.   else
  632.     {
  633.       if (result_list != NULL)
  634.     *result_list = clist;
  635.       return found;
  636.     }
  637. }
  638.  
  639. /* All this hair to move the space to the front of cmdtype */
  640.  
  641. static void
  642. undef_cmd_error (cmdtype, q)
  643.      char *cmdtype, *q;
  644. {
  645.   error ("Undefined %scommand: \"%s\".  Try \"help%s%.*s\".",
  646.     cmdtype,
  647.     q,
  648.     *cmdtype? " ": "",
  649.     strlen(cmdtype)-1,
  650.     cmdtype);
  651. }
  652.  
  653. /* Look up the contents of *LINE as a command in the command list LIST.
  654.    LIST is a chain of struct cmd_list_element's.
  655.    If it is found, return the struct cmd_list_element for that command
  656.    and update *LINE to point after the command name, at the first argument.
  657.    If not found, call error if ALLOW_UNKNOWN is zero
  658.    otherwise (or if error returns) return zero.
  659.    Call error if specified command is ambiguous,
  660.    unless ALLOW_UNKNOWN is negative.
  661.    CMDTYPE precedes the word "command" in the error message.
  662.  
  663.    If INGNORE_HELP_CLASSES is nonzero, ignore any command list
  664.    elements which are actually help classes rather than commands (i.e.
  665.    the function field of the struct cmd_list_element is 0).  */
  666.  
  667. struct cmd_list_element *
  668. lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes)
  669.      char **line;
  670.      struct cmd_list_element *list;
  671.      char *cmdtype;
  672.      int allow_unknown;
  673.      int ignore_help_classes;
  674. {
  675.   struct cmd_list_element *last_list = 0;
  676.   struct cmd_list_element *c =
  677.     lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
  678.   char *ptr = (*line) + strlen (*line) - 1;
  679.  
  680.   /* Clear off trailing whitespace.  */
  681.   while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
  682.     ptr--;
  683.   *(ptr + 1) = '\0';
  684.   
  685.   if (!c)
  686.     {
  687.       if (!allow_unknown)
  688.     {
  689.       if (!*line)
  690.         error ("Lack of needed %scommand", cmdtype);
  691.       else
  692.         {
  693.           char *p = *line, *q;
  694.  
  695.           while (isalnum(*p) || *p == '-')
  696.         p++;
  697.  
  698.           q = (char *) alloca (p - *line + 1);
  699.           strncpy (q, *line, p - *line);
  700.           q[p-*line] = '\0';
  701.           undef_cmd_error (cmdtype, q);
  702.         }
  703.     }
  704.       else
  705.     return 0;
  706.     }
  707.   else if (c == (struct cmd_list_element *) -1)
  708.     {
  709.       /* Ambigous.  Local values should be off prefixlist or called
  710.      values.  */
  711.       int local_allow_unknown = (last_list ? last_list->allow_unknown :
  712.                  allow_unknown);
  713.       char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
  714.       struct cmd_list_element *local_list =
  715.     (last_list ? *(last_list->prefixlist) : list);
  716.       
  717.       if (local_allow_unknown < 0)
  718.     {
  719.       if (last_list)
  720.         return last_list;    /* Found something.  */
  721.       else
  722.         return 0;        /* Found nothing.  */
  723.     }
  724.       else
  725.     {
  726.       /* Report as error.  */
  727.       int amb_len;
  728.       char ambbuf[100];
  729.  
  730.       for (amb_len = 0;
  731.            ((*line)[amb_len] && (*line)[amb_len] != ' '
  732.         && (*line)[amb_len] != '\t');
  733.            amb_len++)
  734.         ;
  735.       
  736.       ambbuf[0] = 0;
  737.       for (c = local_list; c; c = c->next)
  738.         if (!strncmp (*line, c->name, amb_len))
  739.           {
  740.         if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf)
  741.           {
  742.             if (strlen (ambbuf))
  743.               strcat (ambbuf, ", ");
  744.             strcat (ambbuf, c->name);
  745.           }
  746.         else
  747.           {
  748.             strcat (ambbuf, "..");
  749.             break;
  750.           }
  751.           }
  752.       error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
  753.          *line, ambbuf);
  754.       return 0;        /* lint */
  755.     }
  756.     }
  757.   else
  758.     {
  759.       /* We've got something.  It may still not be what the caller
  760.          wants (if this command *needs* a subcommand).  */
  761.       while (**line == ' ' || **line == '\t')
  762.     (*line)++;
  763.  
  764.       if (c->prefixlist && **line && !c->allow_unknown)
  765.     undef_cmd_error (c->prefixname, *line);
  766.  
  767.       /* Seems to be what he wants.  Return it.  */
  768.       return c;
  769.     }
  770.   return 0;
  771. }
  772.     
  773. #if 0
  774. /* Look up the contents of *LINE as a command in the command list LIST.
  775.    LIST is a chain of struct cmd_list_element's.
  776.    If it is found, return the struct cmd_list_element for that command
  777.    and update *LINE to point after the command name, at the first argument.
  778.    If not found, call error if ALLOW_UNKNOWN is zero
  779.    otherwise (or if error returns) return zero.
  780.    Call error if specified command is ambiguous,
  781.    unless ALLOW_UNKNOWN is negative.
  782.    CMDTYPE precedes the word "command" in the error message.  */
  783.  
  784. struct cmd_list_element *
  785. lookup_cmd (line, list, cmdtype, allow_unknown)
  786.      char **line;
  787.      struct cmd_list_element *list;
  788.      char *cmdtype;
  789.      int allow_unknown;
  790. {
  791.   register char *p;
  792.   register struct cmd_list_element *c, *found;
  793.   int nfound;
  794.   char ambbuf[100];
  795.   char *processed_cmd;
  796.   int i, cmd_len;
  797.  
  798.   /* Skip leading whitespace.  */
  799.  
  800.   while (**line == ' ' || **line == '\t')
  801.     (*line)++;
  802.  
  803.   /* Clear out trailing whitespace.  */
  804.  
  805.   p = *line + strlen (*line);
  806.   while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
  807.     p--;
  808.   *p = 0;
  809.  
  810.   /* Find end of command name.  */
  811.  
  812.   p = *line;
  813.   while (*p == '-' || isalnum(*p))
  814.     p++;
  815.  
  816.   /* Look up the command name.
  817.      If exact match, keep that.
  818.      Otherwise, take command abbreviated, if unique.  Note that (in my
  819.      opinion) a null string does *not* indicate ambiguity; simply the
  820.      end of the argument.  */
  821.  
  822.   if (p == *line)
  823.     {
  824.       if (!allow_unknown)
  825.     error ("Lack of needed %scommand", cmdtype);
  826.       return 0;
  827.     }
  828.   
  829.   /* Copy over to a local buffer, converting to lowercase on the way.
  830.      This is in case the command being parsed is a subcommand which
  831.      doesn't match anything, and that's ok.  We want the original
  832.      untouched for the routine of the original command.  */
  833.   
  834.   processed_cmd = (char *) alloca (p - *line + 1);
  835.   for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
  836.     {
  837.       char x = (*line)[cmd_len];
  838.       if (isupper(x))
  839.     processed_cmd[cmd_len] = tolower(x);
  840.       else
  841.     processed_cmd[cmd_len] = x;
  842.     }
  843.   processed_cmd[cmd_len] = '\0';
  844.  
  845.   /* Check all possibilities in the current command list.  */
  846.   found = 0;
  847.   nfound = 0;
  848.   for (c = list; c; c = c->next)
  849.     {
  850.       if (!strncmp (processed_cmd, c->name, cmd_len))
  851.     {
  852.       found = c;
  853.       nfound++;
  854.       if (c->name[cmd_len] == 0)
  855.         {
  856.           nfound = 1;
  857.           break;
  858.         }
  859.     }
  860.     }
  861.  
  862.   /* Report error for undefined command name.  */
  863.  
  864.   if (nfound != 1)
  865.     {
  866.       if (nfound > 1 && allow_unknown >= 0)
  867.     {
  868.       ambbuf[0] = 0;
  869.       for (c = list; c; c = c->next)
  870.         if (!strncmp (processed_cmd, c->name, cmd_len))
  871.           {
  872.         if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
  873.           {
  874.             if (strlen (ambbuf))
  875.               strcat (ambbuf, ", ");
  876.             strcat (ambbuf, c->name);
  877.           }
  878.         else
  879.           {
  880.             strcat (ambbuf, "..");
  881.             break;
  882.           }
  883.           }
  884.       error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
  885.          processed_cmd, ambbuf);
  886.     }
  887.       else if (!allow_unknown)
  888.     error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
  889.       return 0;
  890.     }
  891.  
  892.   /* Skip whitespace before the argument.  */
  893.  
  894.   while (*p == ' ' || *p == '\t') p++;
  895.   *line = p;
  896.  
  897.   if (found->prefixlist && *p)
  898.     {
  899.       c = lookup_cmd (line, *found->prefixlist, found->prefixname,
  900.               found->allow_unknown);
  901.       if (c)
  902.     return c;
  903.     }
  904.  
  905.   return found;
  906. }
  907. #endif
  908.  
  909. /* Helper function for SYMBOL_COMPLETION_FUNCTION.  */
  910.  
  911. /* Return a vector of char pointers which point to the different
  912.    possible completions in LIST of TEXT.  
  913.  
  914.    WORD points in the same buffer as TEXT, and completions should be
  915.    returned relative to this position.  For example, suppose TEXT is "foo"
  916.    and we want to complete to "foobar".  If WORD is "oo", return
  917.    "oobar"; if WORD is "baz/foo", return "baz/foobar".  */
  918.  
  919. char **
  920. complete_on_cmdlist (list, text, word)
  921.      struct cmd_list_element *list;
  922.      char *text;
  923.      char *word;
  924. {
  925.   struct cmd_list_element *ptr;
  926.   char **matchlist;
  927.   int sizeof_matchlist;
  928.   int matches;
  929.   int textlen = strlen (text);
  930.  
  931.   sizeof_matchlist = 10;
  932.   matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
  933.   matches = 0;
  934.  
  935.   for (ptr = list; ptr; ptr = ptr->next)
  936.     if (!strncmp (ptr->name, text, textlen)
  937.     && !ptr->abbrev_flag
  938.     && (ptr->function.cfunc
  939.         || ptr->prefixlist))
  940.       {
  941.     if (matches == sizeof_matchlist)
  942.       {
  943.         sizeof_matchlist *= 2;
  944.         matchlist = (char **) xrealloc ((char *)matchlist,
  945.                         (sizeof_matchlist
  946.                          * sizeof (char *)));
  947.       }
  948.  
  949.     matchlist[matches] = (char *) 
  950.       xmalloc (strlen (word) + strlen (ptr->name) + 1);
  951.     if (word == text)
  952.       strcpy (matchlist[matches], ptr->name);
  953.     else if (word > text)
  954.       {
  955.         /* Return some portion of ptr->name.  */
  956.         strcpy (matchlist[matches], ptr->name + (word - text));
  957.       }
  958.     else
  959.       {
  960.         /* Return some of text plus ptr->name.  */
  961.         strncpy (matchlist[matches], word, text - word);
  962.         matchlist[matches][text - word] = '\0';
  963.         strcat (matchlist[matches], ptr->name);
  964.       }
  965.     ++matches;
  966.       }
  967.  
  968.   if (matches == 0)
  969.     {
  970.       free ((PTR)matchlist);
  971.       matchlist = 0;
  972.     }
  973.   else
  974.     {
  975.       matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1)
  976.                         * sizeof (char *)));
  977.       matchlist[matches] = (char *) 0;
  978.     }
  979.  
  980.   return matchlist;
  981. }
  982.  
  983. static int
  984. parse_binary_operation (arg)
  985.      char *arg;
  986. {
  987.   int length;
  988.  
  989.   if (!arg || !*arg)
  990.     return 1;
  991.  
  992.   length = strlen (arg);
  993.  
  994.   while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
  995.     length--;
  996.  
  997.   if (!strncmp (arg, "on", length)
  998.       || !strncmp (arg, "1", length)
  999.       || !strncmp (arg, "yes", length))
  1000.     return 1;
  1001.   else
  1002.     if (!strncmp (arg, "off", length)
  1003.     || !strncmp (arg, "0", length)
  1004.     || !strncmp (arg, "no", length))
  1005.       return 0;
  1006.     else 
  1007.       {
  1008.     error ("\"on\" or \"off\" expected.");
  1009.     return 0;
  1010.       }
  1011. }
  1012.  
  1013. /* Do a "set" or "show" command.  ARG is NULL if no argument, or the text
  1014.    of the argument, and FROM_TTY is nonzero if this command is being entered
  1015.    directly by the user (i.e. these are just like any other
  1016.    command).  C is the command list element for the command.  */
  1017. void
  1018. do_setshow_command (arg, from_tty, c)
  1019.      char *arg;
  1020.      int from_tty;
  1021.      struct cmd_list_element *c;
  1022. {
  1023.   if (c->type == set_cmd)
  1024.     {
  1025.       switch (c->var_type)
  1026.     {
  1027.     case var_string:
  1028.       {
  1029.         char *new;
  1030.         char *p;
  1031.         char *q;
  1032.         int ch;
  1033.         
  1034.         if (arg == NULL)
  1035.           arg = "";
  1036.         new = (char *) xmalloc (strlen (arg) + 2);
  1037.         p = arg; q = new;
  1038.         while ((ch = *p++) != '\000')
  1039.           {
  1040.         if (ch == '\\')
  1041.           {
  1042.             /* \ at end of argument is used after spaces
  1043.                so they won't be lost.  */
  1044.             if (*p == 0)
  1045.               break;
  1046.             ch = parse_escape (&p);
  1047.             if (ch == 0)
  1048.               break; /* C loses */
  1049.             else if (ch > 0)
  1050.               *q++ = ch;
  1051.           }
  1052.         else
  1053.           *q++ = ch;
  1054.           }
  1055.         if (*(p - 1) != '\\')
  1056.           *q++ = ' ';
  1057.         *q++ = '\0';
  1058.         new = (char *) xrealloc (new, q - new);
  1059.         if (*(char **)c->var != NULL)
  1060.           free (*(char **)c->var);
  1061.         *(char **) c->var = new;
  1062.       }
  1063.       break;
  1064.     case var_string_noescape:
  1065.       if (arg == NULL)
  1066.         arg = "";
  1067.       if (*(char **)c->var != NULL)
  1068.         free (*(char **)c->var);
  1069.       *(char **) c->var = savestring (arg, strlen (arg));
  1070.       break;
  1071.     case var_filename:
  1072.       if (arg == NULL)
  1073.         error_no_arg ("filename to set it to.");
  1074.       if (*(char **)c->var != NULL)
  1075.         free (*(char **)c->var);
  1076.       *(char **)c->var = tilde_expand (arg);
  1077.       break;
  1078.     case var_boolean:
  1079.       *(int *) c->var = parse_binary_operation (arg);
  1080.       break;
  1081.     case var_uinteger:
  1082.       if (arg == NULL)
  1083.         error_no_arg ("integer to set it to.");
  1084.       *(unsigned int *) c->var = parse_and_eval_address (arg);
  1085.       if (*(unsigned int *) c->var == 0)
  1086.         *(unsigned int *) c->var = UINT_MAX;
  1087.       break;
  1088.     case var_integer:
  1089.       {
  1090.         unsigned int val;
  1091.         if (arg == NULL)
  1092.           error_no_arg ("integer to set it to.");
  1093.         val = parse_and_eval_address (arg);
  1094.         if (val == 0)
  1095.           *(int *) c->var = INT_MAX;
  1096.         else if (val >= INT_MAX)
  1097.           error ("integer %u out of range", val);
  1098.         else
  1099.           *(int *) c->var = val;
  1100.         break;
  1101.       }
  1102.     case var_zinteger:
  1103.       if (arg == NULL)
  1104.         error_no_arg ("integer to set it to.");
  1105.       *(int *) c->var = parse_and_eval_address (arg);
  1106.       break;
  1107.     default:
  1108.       error ("gdb internal error: bad var_type in do_setshow_command");
  1109.     }
  1110.     }
  1111.   else if (c->type == show_cmd)
  1112.     {
  1113.       /* Print doc minus "show" at start.  */
  1114.       print_doc_line (gdb_stdout, c->doc + 5);
  1115.       
  1116.       fputs_filtered (" is ", gdb_stdout);
  1117.       wrap_here ("    ");
  1118.       switch (c->var_type)
  1119.     {
  1120.       case var_string:
  1121.     {
  1122.       unsigned char *p;
  1123.       fputs_filtered ("\"", gdb_stdout);
  1124.       for (p = *(unsigned char **) c->var; *p != '\0'; p++)
  1125.         gdb_printchar (*p, gdb_stdout, '"');
  1126.       fputs_filtered ("\"", gdb_stdout);
  1127.     }
  1128.     break;
  1129.       case var_string_noescape:
  1130.       case var_filename:
  1131.     fputs_filtered ("\"", gdb_stdout);
  1132.     fputs_filtered (*(char **) c->var, gdb_stdout);
  1133.     fputs_filtered ("\"", gdb_stdout);
  1134.     break;
  1135.       case var_boolean:
  1136.     fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
  1137.     break;
  1138.       case var_uinteger:
  1139.     if (*(unsigned int *) c->var == UINT_MAX) {
  1140.       fputs_filtered ("unlimited", gdb_stdout);
  1141.       break;
  1142.     }
  1143.     /* else fall through */
  1144.       case var_zinteger:
  1145.     fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
  1146.     break;
  1147.       case var_integer:
  1148.     if (*(int *) c->var == INT_MAX)
  1149.       {
  1150.         fputs_filtered ("unlimited", gdb_stdout);
  1151.       }
  1152.     else
  1153.       fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
  1154.     break;
  1155.         
  1156.       default:
  1157.     error ("gdb internal error: bad var_type in do_setshow_command");
  1158.       }
  1159.       fputs_filtered (".\n", gdb_stdout);
  1160.     }
  1161.   else
  1162.     error ("gdb internal error: bad cmd_type in do_setshow_command");
  1163.   (*c->function.sfunc) (NULL, from_tty, c);
  1164. }
  1165.  
  1166. /* Show all the settings in a list of show commands.  */
  1167.  
  1168. void
  1169. cmd_show_list (list, from_tty, prefix)
  1170.      struct cmd_list_element *list;
  1171.      int from_tty;
  1172.      char *prefix;
  1173. {
  1174.   for (; list != NULL; list = list->next) {
  1175.     /* If we find a prefix, run its list, prefixing our output by its
  1176.        prefix (with "show " skipped).  */
  1177.     if (list->prefixlist && !list->abbrev_flag)
  1178.       cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
  1179.     if (list->type == show_cmd)
  1180.       {
  1181.     fputs_filtered (prefix, gdb_stdout);
  1182.     fputs_filtered (list->name, gdb_stdout);
  1183.     fputs_filtered (":  ", gdb_stdout);
  1184.     do_setshow_command ((char *)NULL, from_tty, list);
  1185.       }
  1186.   }
  1187. }
  1188.  
  1189. /* ARGSUSED */
  1190. static void
  1191. shell_escape (arg, from_tty)
  1192.      char *arg;
  1193.      int from_tty;
  1194. {
  1195. #ifdef CANT_FORK
  1196.   /* FIXME: what about errors (I don't know how GO32 system() handles
  1197.      them)?  */
  1198.   system (arg);
  1199. #else /* Can fork.  */
  1200.   int rc, status, pid;
  1201.   char *p, *user_shell;
  1202.  
  1203.   if ((user_shell = (char *) getenv ("SHELL")) == NULL)
  1204.     user_shell = "/bin/sh";
  1205.  
  1206.   /* Get the name of the shell for arg0 */
  1207.   if ((p = strrchr (user_shell, '/')) == NULL)
  1208.     p = user_shell;
  1209.   else
  1210.     p++;            /* Get past '/' */
  1211.  
  1212.   if ((pid = fork()) == 0)
  1213.     {
  1214.       if (!arg)
  1215.     execl (user_shell, p, 0);
  1216.       else
  1217.     execl (user_shell, p, "-c", arg, 0);
  1218.  
  1219.       fprintf_unfiltered (gdb_stderr, "Exec of shell failed\n");
  1220.       exit (0);
  1221.     }
  1222.  
  1223.   if (pid != -1)
  1224.     while ((rc = wait (&status)) != pid && rc != -1)
  1225.       ;
  1226.   else
  1227.     error ("Fork failed");
  1228. #endif /* Can fork.  */
  1229. }
  1230.  
  1231. static void
  1232. make_command (arg, from_tty)
  1233.      char *arg;
  1234.      int from_tty;
  1235. {
  1236.   char *p;
  1237.  
  1238.   if (arg == 0)
  1239.     p = "make";
  1240.   else
  1241.     {
  1242.       p = xmalloc (sizeof("make ") + strlen(arg));
  1243.       strcpy (p, "make ");
  1244.       strcpy (p + sizeof("make ")-1, arg);
  1245.     }
  1246.   
  1247.   shell_escape (p, from_tty);
  1248. }
  1249.  
  1250. static void
  1251. show_user_1 (c, stream)
  1252.      struct cmd_list_element *c;
  1253.      GDB_FILE *stream;
  1254. {
  1255.   register struct command_line *cmdlines;
  1256.  
  1257.   cmdlines = c->user_commands;
  1258.   if (!cmdlines)
  1259.     return;
  1260.   fputs_filtered ("User command ", stream);
  1261.   fputs_filtered (c->name, stream);
  1262.   fputs_filtered (":\n", stream);
  1263.   while (cmdlines)
  1264.     {
  1265.       fputs_filtered (cmdlines->line, stream); 
  1266.       fputs_filtered ("\n", stream); 
  1267.       cmdlines = cmdlines->next;
  1268.     }
  1269.   fputs_filtered ("\n", stream);
  1270. }
  1271.  
  1272. /* ARGSUSED */
  1273. static void
  1274. show_user (args, from_tty)
  1275.      char *args;
  1276.      int from_tty;
  1277. {
  1278.   struct cmd_list_element *c;
  1279.   extern struct cmd_list_element *cmdlist;
  1280.  
  1281.   if (args)
  1282.     {
  1283.       c = lookup_cmd (&args, cmdlist, "", 0, 1);
  1284.       if (c->class != class_user)
  1285.     error ("Not a user command.");
  1286.       show_user_1 (c, gdb_stdout);
  1287.     }
  1288.   else
  1289.     {
  1290.       for (c = cmdlist; c; c = c->next)
  1291.     {
  1292.       if (c->class == class_user)
  1293.         show_user_1 (c, gdb_stdout);
  1294.     }
  1295.     }
  1296. }
  1297.  
  1298. void
  1299. _initialize_command ()
  1300. {
  1301.   add_com ("shell", class_support, shell_escape,
  1302.        "Execute the rest of the line as a shell command.  \n\
  1303. With no arguments, run an inferior shell.");
  1304.   add_com ("make", class_support, make_command,
  1305.        "Run the ``make'' program using the rest of the line as arguments.");
  1306.   add_cmd ("user", no_class, show_user, 
  1307.        "Show definitions of user defined commands.\n\
  1308. Argument is the name of the user defined command.\n\
  1309. With no argument, show definitions of all user defined commands.", &showlist);
  1310. }
  1311.